package com.android.djlmapp.activity.msg;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.html.URLImageGetter;
import com.android.djlmapp.model.msg.InstationMsgDetailModel;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;

import static com.android.djlmapp.application.UILOption.options;

/**
 * @Author wangziheng
 * @ProjectName djlm
 * @ClassName MsgDetailActivity
 * @package com.android.djlmapp.activity.msg
 * @data 2019/2/15 16:01
 * @Describe
 * @Change
 */
public class MsgDetailActivity extends BaseActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;

    @BindView(R.id.msgContent_title)
    TextView mMsgTitleTv;
    @BindView(R.id.msgContent_content)
    TextView mMsgContentTv;
    private Unbinder mUnbinder;
    /**
     * 是否是站内信详情
     */
    private boolean isInstationMsg = false;
    /**
     * 消息类型key值
     */
    public static final String MSG_DETAIL_TYPE_CODE = "MSG_DETAIL_TYPE_CODE";
    /**
     * 消息id  站内信使用
     */
    public static final String INSTATION_MSG_ID_CODE = "INSTATION_MSG_ID_CODE";
    private String mMsgTitle, mMsgContent, mMsgID;

    @BindView(R.id.msg_detail_content_layout)
    LinearLayout mContentLayout;


    @BindView(R.id.null_data_layout_txt)
    TextView mNullDataWarningTv;
    @BindView(R.id.null_data_layout)
    RelativeLayout mNullDataLayout;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectNetErrorLayout;
    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_content);
        mUnbinder = ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        tvName.setText("消息详情");
        ivTopMsg.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        Intent mIntent = getIntent();
        isInstationMsg = mIntent.getBooleanExtra(MSG_DETAIL_TYPE_CODE, false);
        mMsgID = mIntent.getStringExtra(INSTATION_MSG_ID_CODE);
        if (isInstationMsg) {
            getMsgDetail();
        } else {
            getSystemMSgDetail();
            mMsgTitle = mIntent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            mMsgContent = mIntent.getStringExtra(JPushInterface.EXTRA_ALERT);
            mMsgContentTv.setText(mMsgContent);
            mMsgTitleTv.setText(mMsgTitle);
        }

    }

    /**
     * 获取站内信消息详情
     */

    private void getMsgDetail() {
        MyApplication.getInstance().getHttpTools().getInstationMsgDetail(mMsgID, new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if(isFinishing()){
                    return;
                }
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    InstationMsgDetailModel detailModel = gson.fromJson(result, InstationMsgDetailModel.class);
                    if (detailModel != null) {
                        if (detailModel.getCode() == 200) {
                            mMsgTitle = detailModel.getData().getMsg_title();
                            mMsgContent = detailModel.getData().getContent();

                            options = new DisplayImageOptions.Builder()
                                    .showImageOnLoading(R.mipmap.data_load_error)
                                    .showImageForEmptyUri(R.mipmap.data_load_error)
                                    .showImageOnFail(R.mipmap.data_load_error)
                                    .cacheInMemory(true)
                                    .cacheOnDisk(true)
                                    .considerExifParams(true)
                                    .bitmapConfig(Bitmap.Config.RGB_565)
                                    .displayer(new FadeInBitmapDisplayer(300))
                                    .build();
                            CharSequence charSequence = Html.fromHtml(mMsgContent,
                                    new URLImageGetter(mMsgContent, MsgDetailActivity.this,
                                            mMsgContentTv, options), null);
                            mMsgContentTv.setText(charSequence);
                            mMsgTitleTv.setText(mMsgTitle);
                            normalLayout();
                        } else {
                            noDataLayout();
                        }
                    } else {
                        noDataLayout();
                    }
                } else {
                    noDataLayout();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                connectNetErrorLayout();
            }
        });
    }


    /**
     * 获取系统消息详情
     * 未读消息修改成已读
     */
    private void getSystemMSgDetail(){
        MyApplication.getInstance().getHttpTools().getSystemMsgDetail(mMsgID, new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {}
        });
    }

    /**
     * 无数据状态
     */
    private void noDataLayout() {
        if (mNullDataLayout.getVisibility() == View.GONE) {
            mConnectNetErrorLayout.setVisibility(View.GONE);
            mNullDataLayout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 网络错误状态
     */
    private void connectNetErrorLayout() {
        if (mConnectNetErrorLayout.getVisibility() == View.GONE) {
            mNullDataLayout.setVisibility(View.GONE);
            mConnectNetErrorLayout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 正常布局状态
     */
    private void normalLayout() {
        if (mConnectNetErrorLayout.getVisibility() == View.VISIBLE || mNullDataLayout.getVisibility() == View.VISIBLE) {
            mConnectNetErrorLayout.setVisibility(View.GONE);
            mNullDataLayout.setVisibility(View.GONE);
        }
    }
}
